<template>
<view>
<view class="top" style="background:url(./../img/address-bg-bd.pn) no-repeat"></view>
<view class="content">
   <view class="assa" @tap="site">
   <view class="dizhi" v-if="address.length != 0">
      <view class="left_img">
         <image src="/static/remao_shop/img/dingwei.png"></image>
      </view>
      <view class="left">
         <view> 姓名：</view>  
         <view class="shdz">收货地址：</view>
         <!-- <view class='moren' wx:if="{{address.city != undefinde}}">默认</view> -->
      </view>
      <view class="conter">
         <view class="conter_phone"><text>{{address.name}}</text> {{address.mobile}}</view>
         <view class="conter_site shdz">{{address.addr}}{{address.address}}</view>
      </view>
         <view class="right">
            <view>
               <image src="/static/remao_shop/img/arrow_icon.png"></image>
            </view>
         </view>
   </view>
   <view class="please" v-if="address.length== 0">请选择收货地址..</view>
</view>
</view>
<view class="cont">
   <image :src="shopList.thumb_img"></image>
   <text>{{shopList.goods_name}}</text>
   <view>{{shopList.price}}积分</view>
</view>
<view class="bottom">
   <text>实付: {{shopList.price}}积分</text><view v-if="shopList.money > 0"> + {{shopList.money}}元 </view> 
   <button hover-class="none" @tap="exchange">立即兑换</button>
</view>
</view>
</template>

<script>
// hotmall/packageB/IntegralOrder/IntegralOrder.js
var app = getApp();
import api from "../../../api";

export default {
  data() {
    return {
      uid: '',
      integral_id: '',
      address: [],
      shopList: [],
      address_id: '',
      order_sn: ''
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getStorage({
      key: 'userId',
      success: res => {
        console.log(res);
        this.setData({
          uid: res.data
        });
        this.getList();
      }
    });
    console.log(options);
    this.uid = options.uid;
    this.integral_id = options.integral_id;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'username',

      success(res) {
        console.log(res.data);
        that.setData({
          address: res.data
        });
      }

    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    getList: function () {
      app.globalData.request({
        url: api.default.integral_integralorder,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          uid: this.uid,
          integral_id: this.integral_id
        },
        method: 'POST',
        success: res => {
          console.log(res);
          this.setData({
            address: res.address,
            shopList: res.integral_order
          });
        }
      });
    },
    //立即兑换
    exList: function () {
      console.log(this.address.address_id);
      app.globalData.request({
        url: api.default.integral_exchanges,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          integral_id: this.integral_id,
          uid: this.uid,
          address_id: this.address.address_id,
          money: this.shopList.money,
          uid: this.uid,
          message: ''
        },
        method: 'POST',
        success: res => {
          console.log(res);

          if (res.status == 1) {
            this.order_sn = res.order_id;

            if (this.shopList.money > 0) {
              this.weixinPayMoney();
            }

            wx.showToast({
              title: res.msg
            });
            setTimeout(function () {
              wx.navigateBack({
                delta: 3
              });
            }, 1500);
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none'
            });
          }
        }
      });
    },
    exchange: function () {
      var that = this;
      console.log(this.address);

      if (this.address.length != 0) {
        wx.showModal({
          title: '温馨提示',
          content: '是否要支付' + this.shopList.price + '积分' + this.shopList.money + '元',

          success(res) {
            if (res.confirm) {
              wx.showLoading({
                title: '加载中'
              });
              setTimeout(function () {
                wx.hideLoading();
              }, 1000);
              that.exList();
            } else if (res.cancel) {
              console.log('用户点击取消');
            }
          }

        });
      } else {
        wx.showToast({
          title: '请选择收货地址',
          icon: 'none'
        });
      }
    },
    // 微信支付
    weixinPayMoney: function () {
      var _that = this;

      console.log(_that.order_sn);
      console.log(_that.shopList.money);
      console.log(wx.getStorageSync("openid"));
      app.globalData.request({
        url: api.fyz.weixinPay,
        data: {
          type: 6,
          pay_sn: _that.order_sn,
          money: _that.shopList.money,
          //money: 20,
          openid: wx.getStorageSync("openid")
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        method: 'POST',
        success: res => {
          console.log(res);
          var prepayid = res.ioswx_config.prepayid,
              osn = res.osn;
          console.log('支付id:', prepayid); //  付款成功

          if (res.status == 1) {
            wx.requestPayment({
              appId: res.weixin_config.appid,
              nonceStr: res.weixin_config.nonce_str,
              package: 'prepay_id=' + res.weixin_config.prepay_id,
              signType: 'MD5',
              timeStamp: res.weixin_config.timestamp,
              paySign: res.weixin_config.sign,
              success: function (res1) {
                wx.getStorage({
                  key: 'userId',
                  success: function (aaa) {
                    console.log(aaa.data);
                    var open = aaa.data;
                    console.log(open);
                    app.globalData.request({
                      url: api.gzSea.message,
                      //仅为示例，并非真实的接口地址
                      data: {
                        sole: prepayid,
                        order_sn: osn,
                        user_id: open
                      },
                      method: 'POST',
                      header: {
                        'content-type': 'application/x-www-form-urlencoded' // 默认值

                      },

                      success(res) {
                        console.log(res);
                      }

                    });
                  }
                }); //  更新订单状态

                app.globalData.request({
                  url: api.default.integral_order_update_pay,
                  data: {
                    order_id: this.order_sn
                  },
                  header: {
                    'content-type': 'application/x-www-form-urlencoded' // 默认值

                  },
                  method: 'POST',
                  success: res => {
                    console.log(res);

                    if (res.status == 1) {
                      // wx.showToast({
                      //    title: res.msg,
                      // })
                      setTimeout(function () {
                        wx.navigateBack({
                          delta: 2
                        });
                      }, 500); // wx.redirectTo({
                      //   url: '../../packageB/order/order?status=0&userId=' + _that.data.userId,
                      // })
                      // wx.removeStorageSync('couponId')
                    } else {
                      wx.showToast({
                        title: res.msg,
                        duration: 1000
                      });
                    }
                  }
                });
              },
              fail: function (res2) {
                wx.showToast({
                  title: '支付失败',
                  icon: 'none',
                  duration: 1000
                });
              }
            });
          }
        },
        fail: function (ret) {
          console.log(ret);
        },
        complete: function (reb) {
          console.log(reb);
        }
      });
    },
    site: function () {
      var that = this;
      wx.navigateTo({
        url: '../../packageB/address/address?integral=' + 'integral'
      });
      console.log(this.address);
    }
  }
};
</script>
<style>
@import "./IntegralOrder.css";
</style>